
let express = require('express');
let router = express.Router();
let filter = require('../../components/filter');
    
/**
 * 查询档案数据 2018-10-18
 *
 * 按条件查询档案数据
 *
 */
router.get('/:code', function (req, res, next) {
    let params = req.query;
    var limits = "";
    var sql = "";
    var datas = [];

    if (req.params.code === undefined || req.params.code === "") {
        res.json({"success": false, "message": "档案所属机构编码不正确！"});
        return;
    }

    let isDelete = 0;
    if (params.isDelete !== undefined && params.isDelete !== "") {
        isDelete = 1;
    }

    sql = " where isDelete = ? and fatherId = 0";
    datas.push(isDelete);

    if (params.year !== undefined && params.year !== "") {
        sql += " and year = ?";
        datas.push(params.year);
    }

    if (params.classifyCode !== undefined && params.classifyCode !== "") {
        sql += " and classifyCode = ?";
        datas.push(params.classifyCode);
    }

    if (params.departmentName !== undefined && params.departmentName !== "") {
        sql += " and departmentName = ?";
        datas.push(params.departmentName);
    }

    if (params.title !== undefined && params.title !== "") {
        sql += " and title like ?";
        datas.push("%" + params.title + "%");
    }


    req.app.db.query("select id, classifyNo from s_" + req.params.code + " "  + sql + " order by classifyNo " +  limits, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "data": results, "totalCount": results.length, "message": ""});
    });

});

router.get('/:code/basearchives', function (req, res, next) {
    let params = req.query;
    var limits = "";
    var sql = "";
    var datas = [];

    if (req.params.code === undefined || req.params.code === "") {
        res.json({"success": false, "message": "档案所属机构编码不正确！"});
        return;
    }

    let isDelete = 0;
    if (params.isDelete !== undefined && params.isDelete !== "") {
        isDelete = 1;
    }

    sql = " where isDelete = ?";
    datas.push(isDelete);

    if (params.year !== undefined && params.year !== "") {
        sql += " and year = ?";
        datas.push(params.year);
    }

    if (params.classifyCode !== undefined && params.classifyCode !== "") {
        sql += " and classifyCode = ?";
        datas.push(params.classifyCode);
    }

    if (params.departmentName !== undefined && params.departmentName !== "") {
        sql += " and departmentName = ?";
        datas.push(params.departmentName);
    }

    if (params.printAll !== undefined && params.printAll) {
        if (params.hasFiles === undefined) {
            sql += " and fatherId = 0";
        }
    }
    else {
        if (params.hasFiles !== undefined) {
            if (params.ids !== undefined && params.ids !== "") {
                sql += " and (id in (" + params.ids + ") or fatherId in (" + params.ids + "))";
            }
        }
        else {
            if (params.ids !== undefined && params.ids !== "") {
                sql += " and id in (" + params.ids + ")";
            }
        }
    }

    req.app.db.query("select id, fatherId, departmentName, rank, classifyNo, fileCode, title, CONCAT(DATE_FORMAT(beginDate,'%Y.%m.%d'), '-', DATE_FORMAT(endDate,'%Y.%m.%d')) as archiveDate, DATE_FORMAT(beginDate,'%Y%m%d') as fileDate, page, person1, storagePeriod " +
        " from s_" + req.params.code + " "  + sql + " order by classifyNo " +  limits, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }
        let data = [];
        for (let i = 0; i < results.length; i ++) {
            if (results[i].fatherId === 0) {
                data.push(results[i]);
            }
            else {
                for (let j = 0; j < data.length; j ++) {
                    if (results[i].fatherId === data[j].id) {
                        if (data[j].childs === undefined) {
                            data[j].childs = [];
                        }
                        data[j].childs.push(results[i])
;                    }
                }
            }
        }
        res.json({"success": true, "data": data, "totalCount": data.length, "message": ""});
    });

});

module.exports = router;
    